<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>余额充值测试</title>
    <style>
        * {
            box-sizing: border-box;
            margin: 0;
            padding: 0;
        }
        body {
            font-family: 'Microsoft YaHei', Arial, sans-serif;
            line-height: 1.6;
            color: #333;
            background-color: #f5f5f5;
            padding: 20px;
        }
        .container {
            max-width: 600px;
            margin: 0 auto;
            background-color: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
        }
        h1 {
            text-align: center;
            margin-bottom: 20px;
            color: #2c3e50;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
        }
        input[type="text"],
        input[type="number"] {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 16px;
        }
        button {
            background-color: #e67e22;
            color: white;
            border: none;
            padding: 10px 20px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
            width: 100%;
        }
        button:hover {
            background-color: #d35400;
        }
        #result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            display: none;
        }
        .success {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }
        .error {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
        .info-box {
            background-color: #e7f3fe;
            border-left: 6px solid #2196F3;
            margin-bottom: 15px;
            padding: 10px;
        }
        .balance-box {
            background-color: #fff3cd;
            border: 1px solid #ffeeba;
            padding: 10px;
            margin-top: 15px;
            border-radius: 4px;
            text-align: center;
        }
        .balance-amount {
            font-size: 24px;
            font-weight: bold;
            color: #ff9800;
        }
        .api-info {
            margin-top: 20px;
            padding: 10px;
            background-color: #f8f9fa;
            border-radius: 4px;
            font-size: 14px;
        }
        .api-info pre {
            background-color: #f1f1f1;
            padding: 8px;
            border-radius: 4px;
            overflow-x: auto;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>校园二手书店 - 余额充值测试</h1>
        
        <div class="info-box">
            <p>此页面用于测试用户余额充值功能。请输入JWT Token和充值金额（必须大于0）进行充值操作。</p>
        </div>
        
        <form id="rechargeForm">
            <div class="form-group">
                <label for="token">JWT Token (必填):</label>
                <input type="text" id="token" name="token" placeholder="Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9..." required>
            </div>
            
            <div class="form-group">
                <label for="amount">充值金额 (必填，大于0):</label>
                <input type="number" id="amount" name="amount" placeholder="请输入充值金额" step="0.01" min="0.01" required>
            </div>
            
            <button type="submit">确认充值</button>
        </form>
        
        <div id="result"></div>
        
        <div class="balance-box" id="balanceBox" style="display: none;">
            <p>当前余额：<span class="balance-amount" id="currentBalance">0.00</span> 元</p>
        </div>
        
        <div class="api-info">
            <h3>接口信息</h3>
            <p><strong>URL:</strong> PUT http://localhost:8080/api/orders/recharge/{rechargeBalance}</p>
            <p><strong>Path参数:</strong> rechargeBalance (充值金额，必须大于0)</p>
            <p><strong>Header参数:</strong> Authorization (JWT令牌)</p>
            <p><strong>功能说明:</strong> 用户ID从token中获取，充值金额与用户余额相加</p>
        </div>
    </div>
    
    <script>
        // 表单提交
        document.getElementById('rechargeForm').addEventListener('submit', async function(e) {
            e.preventDefault();
            
            // 获取JWT Token和充值金额
            const token = document.getElementById('token').value.trim();
            const amount = parseFloat(document.getElementById('amount').value);
            
            // 打印提交参数到控制台
            console.log('==== 提交参数 ====');
            console.log('Token:', token);
            console.log('充值金额:', amount);
            console.log('================');
            
            if (!token) {
                showResult('JWT Token不能为空', false);
                return;
            }
            
            if (isNaN(amount) || amount <= 0) {
                showResult('充值金额必须大于0', false);
                return;
            }
            
            try {
                // 打印请求详情
                console.log('==== 请求详情 ====');
                console.log('请求URL:', `http://localhost:8080/api/orders/recharge/${amount}`);
                console.log('请求方法:', 'PUT');
                console.log('请求头:', {
                    'Authorization': token,
                    'Content-Type': 'application/json'
                });
                console.log('================');
                
                // 发送充值请求
                const response = await fetch(`http://localhost:8080/api/orders/recharge/${amount}`, {
                    method: 'PUT',
                    headers: {
                        'Authorization': token,
                        'Content-Type': 'application/json'
                    }
                });
                
                const result = await response.json();
                console.log('返回结果:', result);
                
                if (result.code === 200) {
                    showResult(`充值成功: ${result.message || '余额已更新'}`, true);
                    
                    // 尝试获取并显示当前余额
                    await fetchCurrentBalance(token);
                } else {
                    showResult(`充值失败: ${result.message || '未知错误'}`, false);
                }
            } catch (error) {
                console.log('请求错误:', error);
                showResult('请求出错: ' + error.message, false);
            }
        });
        
        // 获取当前余额
        async function fetchCurrentBalance(token) {
            try {
                const response = await fetch('http://localhost:8080/api/user/info', {
                    method: 'GET',
                    headers: {
                        'Authorization': token
                    }
                });
                
                const result = await response.json();
                
                if (result.code === 200 && result.data) {
                    if (result.data.balance !== undefined) {
                        document.getElementById('currentBalance').textContent = result.data.balance.toFixed(2);
                        document.getElementById('balanceBox').style.display = 'block';
                    }
                }
            } catch (error) {
                console.error('获取余额失败:', error);
            }
        }
        
        // 显示结果
        function showResult(message, isSuccess) {
            const resultDiv = document.getElementById('result');
            resultDiv.textContent = message;
            resultDiv.className = isSuccess ? 'success' : 'error';
            resultDiv.style.display = 'block';
        }
        
        // 检查URL参数，自动填充表单
        window.addEventListener('DOMContentLoaded', function() {
            const urlParams = new URLSearchParams(window.location.search);
            const token = urlParams.get('token');
            
            if (token) {
                document.getElementById('token').value = token;
                // 尝试获取当前余额
                fetchCurrentBalance(token);
            }
        });
    </script>
</body>
</html> 